<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input
          v-model="query.value"
          clearable
          placeholder="输入搜索内容"
          style="width: 200px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-select v-model="query.type" clearable placeholder="类型" class="filter-item" style="width: 130px">
          <el-option v-for="item in queryTypeOptions" :key="item.key" :label="item.display_name" :value="item.key" />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="500px"
      >
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="责任级别" prop="dutyLevel">
            <!--<el-input v-model="form.deviceType" style="width: 370px;" />-->
            <el-select v-model="form.dutyLevel" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.duty_level"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <!--<el-form-item label="责任级别" prop="dutyLevel">-->
          <!--<el-input v-model="form.dutyLevel" style="width: 370px;" />-->
          <!--</el-form-item>-->
          <el-form-item label="责任名称" prop="dutyName">
            <el-input v-model="form.dutyName" style="width: 370px;" />
          </el-form-item>
          <!--<el-form-item label="责任人">-->
          <!--未设置字典，请手动设置 Select-->
          <!--</el-form-item>-->
          <!--<el-form-item label="责任部门">-->
          <!--未设置字典，请手动设置 Select-->
          <!--</el-form-item>-->

          <el-form-item prop="dutyDeptId" label="负责部门">
            <el-select
              v-model="form.dutyDeptId"
              style="width: 260px"
              placeholder="请选择负责部门"
            >
              <el-option
                v-for="item in depts"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item prop="dutyUserId" label="负责人">
            <el-select
              v-model="form.dutyUserId"
              style="width: 260px"
              placeholder="请选择负责人"
            >
              <el-option
                v-for="item in depManagers"
                :key="item.id"
                :label="item.username"
                :value="item.id"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="责任目标">
            <el-input v-model="form.dutyTarget" :rows="3" type="textarea" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="责任描述">
            <el-input v-model="form.dutyDesc" :rows="3" type="textarea" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="文件路径">
            <el-input v-model="form.filePath" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="制定日期">
            <el-date-picker v-model="form.formulateDate" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="审核日期">
            <el-date-picker v-model="form.auditDate" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="批准日期">
            <el-date-picker v-model="form.approvalDate" type="datetime" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <!--<el-table-column prop="id" label="主键" />-->

        <el-table-column prop="dutyLevel" label="责任级别">
          <template slot-scope="scope">
            {{ dict.label.duty_level[scope.row.dutyLevel] }}
          </template>
        </el-table-column>
        <el-table-column prop="dutyName" label="责任名称" />
        <el-table-column prop="username" label="责任人" />
        <el-table-column prop="deptName" label="责任部门" />
        <el-table-column prop="dutyTarget" label="责任目标" />
        <el-table-column prop="dutyDesc" label="责任描述" />
        <el-table-column prop="filePath" label="文件路径" />
        <el-table-column prop="formulateDate" label="制定日期">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.formulateDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="auditDate" label="审核日期">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.auditDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="approvalDate" label="批准日期">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.approvalDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column
          v-permission="['admin','targetResponsibility:edit','targetResponsibility:del']"
          label="操作"
          width="150px"
          align="center"
        >
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudTargetResponsibility from '@/api/psp/targetResponsibility'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { getDepts } from '@/api/system/dept'
import { getAllUsers } from '@/api/system/user'

const defaultForm = {
  id: null,
  createBy: null,
  updateBy: null,
  createTime: null,
  updateTime: null,
  dutyLevel: null,
  dutyName: null,
  dutyUserId: null,
  dutyDeptId: null,
  dutyTarget: null,
  dutyDesc: null,
  filePath: null,
  formulateDate: null,
  auditDate: null,
  approvalDate: null
}
export default {
  name: 'TargetResponsibility',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['duty_level'],
  cruds() {
    return CRUD({
      title: '目标责任',
      url: 'api/targetResponsibility',
      sort: 'id,desc',
      crudMethod: { ...crudTargetResponsibility }
    })
  },
  data() {
    return {
      permission: {
        add: ['admin', 'targetResponsibility:add'],
        edit: ['admin', 'targetResponsibility:edit'],
        del: ['admin', 'targetResponsibility:del']
      },
      depts: [],
      depManagers: [],
      rules: {
        dutyLevel: [
          { required: true, message: '责任级别不能为空', trigger: 'blur' }
        ],
        dutyName: [
          { required: true, message: '责任名称不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'dutyName', display_name: '责任名称' },
        { key: 'dutyUserId', display_name: '责任人' },
        { key: 'dutyDeptId', display_name: '责任部门' },
        { key: 'formulateDate', display_name: '制定日期' },
        { key: 'auditDate', display_name: '审核日期' },
        { key: 'approvalDate', display_name: '批准日期' }
      ]
    }
  },
  methods: {
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      this.getDepts()
      this.getDepManagers()
      const query = this.query
      if (query.type && query.value) {
        this.crud.params[query.type] = query.value
      }
      return true
    },
    getDepManagers() {
      getAllUsers().then(res => {
        console.log(res.content)
        this.depManagers = res.content
      }).catch(() => { })
    },
    getDepts() {
      getDepts().then(res => {
        this.depts = res.content
      }).catch(() => { })
    }
  }
}
</script>

<style scoped>

</style>
